package com.ce.pms.customer.web;

import java.util.ArrayList;
import java.util.List;

import com.ce.pms.customer.model.ParticipantDO;
import com.ce.pms.customer.service.IMemberService;
import com.ce.pms.customer.vo.CustomerHouseVO;
import com.ce.pms.customer.vo.CustomerVO;
import com.ce.pms.customer.vo.IndividualVO;
import com.ce.pms.customer.vo.InterestVO;
import com.ce.pms.customer.vo.MemberVO;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.annotation.Read;

/**
 * @author huxu
 * @version 2.0
 */

public class MemberAction extends CustomerAction {
	private IMemberService memberService;

	/**
	 * @roseuid 483CC420001F
	 */
	public MemberAction() {

	}
	/**
	 * 方法描述: 验证参与人在该房间内是否已经存在
	 * @author 胡煦
	 * Create on:2008-7-15
	 * @param
	 * @return
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public void checkParticipantIdAndHouseIdExclusive(@Read(key = "ownerId")
			Long ownerId,	@Read(key="participantId") Long participantId,
										@Read(key="houseId") Long houseId){
		Boolean boolean1=memberService.checkMemberby2Id(houseId, participantId, houseId);
		renderText(boolean1 ? "false":"true");
	}
	/**
	 * 增加家庭成员
	 * @param menberVO
	 * @return Long
	 * @roseuid 4821421F0165
	 */
	public Long addMember(@Read
	MemberVO memberVO) {
		memberVO.setCreaterId(this.getUserId());
		memberVO.setID_type(super.getCertifiCateTypeName(memberVO.getID_typeId()));
		memberService.addMember(memberVO);
		this.renderText("true");

		return null;

		//menberVO.setOwnerId((Long) getRequest().getAttribute("participantId"));

	}

	/**
	 * Function: 初始化新增成员信息
	 * @return
	 * Create author:胡煦
	 * Create on:2008-6-6
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public String initMemberAdd(@Read(key = "keyId")
	String keyId) {
		//		初始化下拉列表
		List<CustomerVO> cutomerList = this.getCustomerListForMemberCache();
		setAttribute("customerNameList", cutomerList);

		setAttribute("houseNumList", super.getCustomerHouseListCacheHerebyFlag(
				keyId, true));
		getRequest().setAttribute("certificateTypes", getCertificateType());
		setAttribute("reletionTypeList", getReletionType());
		List<InterestVO> listAll = getAllInterestList();

		getRequest().setAttribute("interestes", listAll);

		return "initMemberAdd";
	}

	/**
	 * @param CustomerId - 业主|租户ID
	 * @return List
	 * @roseuid 482169DB0250
	 */
	public List getCustomerId() {
		List<CustomerVO> cutomerList = this.getCustomerListForMemberCache();
		List<Long> cutomeridList = new ArrayList<Long>();
		for (CustomerVO customerVO : cutomerList) {
			Long l = customerVO.getParticipantId();
			cutomeridList.add(l);
		}
		return cutomeridList;
	}

	/**
	 * 方法描述: 根据页面客户ID List查询其家庭成员信息
	 * @author 胡煦
	 * Create on:2008-6-3
	 * @param
	 * @return 返回当前页的查询结果
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public List<MemberVO> getMemberInfoListBy(String key) {

		List<Long> lid = new ArrayList<Long>();//this.getCustomerId();
		lid = this.getCustomerId();

		List<Long> hid = new ArrayList<Long>();
		hid = this.getHouserIdListForMemberFromCache(key);
		return memberService.findMemberbyListId(lid, hid);
	}

	/**
	 * 方法描述: 根据选中客户ID查询其家庭成员详细信息
	 * @author 胡煦
	 * Create on:2008-6-3
	 * @param
	 * @return 返回当前页的查询结果
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public MemberVO getMemberParticularInfoById(Long ownerId, Long paticpantId,
			Long houseId) {
		List memberList = memberService.findMemberBy(ownerId, houseId);
		if (memberList != null) {
			for (int i = 0; i < memberList.size(); i++) {
				MemberVO m = (MemberVO) memberList.get(i);
				if (m.getParticipantId().equals(paticpantId)) {
					return m;
				}
			}

		}
		return null;

	}


	/**
	 * 方法描述: 显示单条家庭成员详细信息
	 * @author 胡煦
	 * Create on:2008-6-3
	 * @param
	 * @return
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */

	public String showOneMemberParticularInfo(@Read(key = "ownerId")
	Long ownerId, @Read(key = "paticpantId")
	Long paticpantId, @Read(key = "houseId")
	Long houseId) {
		MemberVO memberItem = null;
		if (BlankUtil.isBlank(ownerId) || BlankUtil.isBlank(houseId)) {
			memberItem = new MemberVO();
		} else {
			memberItem = getMemberParticularInfoById(ownerId, paticpantId,
					houseId);
		}

		setAttribute("memberItem", memberItem);
		return "renderMemberParticularInfo";

	}

	/**
	 * 方法描述: 初始化memberBaseList.jsp
	 * @author 胡煦
	 * Create on:2008-6-13
	 * @param nameStr：
	 * @return
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */

	public String initmemberBaseList(@Read
	MemberVO memberVO, @Read(key = "ec_p", defaultValue = "1")
	Integer pageNO, @Read(key = "ec_crd", defaultValue = "5")
	Integer pageSize, @Read(key = "keyId")
	String keyId) {

		List<MemberVO> memberList = this.getMemberInfoListBy(keyId);
		List<MemberVO> memberInfoList = this.paginationList(pageNO, pageSize,
				memberList);
		setAttribute("memberInfoList", memberInfoList);
		setAttribute("totalRows", new Integer(memberList.size()));
		setAttribute("keyId", keyId);
		return "initmemberBaseList";
	}

	/**
	 * 方法描述: 初始化memberBaseList.jsp
	 * @author 胡煦
	 * Create on:2008-6-13
	 * @param nameStr：
	 * @return
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */

	public String initmemberSampleList(@Read
	MemberVO memberVO, @Read(key = "ec_p", defaultValue = "1")
	Integer pageNO, @Read(key = "ec_crd", defaultValue = "5")
	Integer pageSize, @Read(key = "keyId")
	String keyId) {

		List<MemberVO> memberList = this.getMemberInfoListBy(keyId);
		List<MemberVO> memberInfoList = this.paginationList(pageNO, pageSize,
				memberList);
		setAttribute("memberInfoList", memberInfoList);
		setAttribute("totalRows", new Integer(memberList.size()));
		setAttribute("keyId", keyId);
		return "initmemberSampleList";
	}

	/**
	 * 方法描述: 分页显示家庭成员信息
	 * @author 胡煦
	 * Create on:2008-6-3
	 * @param
	 * @return
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	// @Out(key = "memberInfoList")
	public String showMemberInfoList(@Read(key = "keyId")
	String keyId) {
		this.getRequest().setAttribute("keyId", keyId);
		return "renderMemberList";
	}

	/**
	 * 方法描述: 分页显示家庭成员信息
	 * @author 胡煦
	 * Create on:2008-6-3
	 * @param
	 * @return
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	// @Out(key = "memberInfoList")
	public String showMainMemberInfoList(@Read(key = "ownerId")
	Long participantId, @Read(key = "clientType")
	String clientType, @Read(key = "clientId")
	Long clientId) {

		this.getRequest().setAttribute("ownerId", participantId);
		this.getRequest().setAttribute("clientType", clientType);
		this.getRequest().setAttribute("clientId", clientId);
		return "renderMainMemberList";
	}

	/**
	 * 方法描述: 初始化memberBaseList.jsp
	 * @author 胡煦
	 * Create on:2008-6-13
	 * @param nameStr：
	 * @return
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */

	public String initMainMemberBaseList(@Read
	MemberVO memberVO, @Read(key = "ownerId")
	Long participantId, @Read(key = "clientType")
	String clientType, @Read(key = "clientId")
	Long clientId, @Read(key = "ec_p", defaultValue = "1")
	Integer pageNO, @Read(key = "ec_crd", defaultValue = "5")
	Integer pageSize) {
		if(BlankUtil.isBlank(clientId)){
			clientId = (Long) this.getRequest().getAttribute("clientId");
		}
		List houseList = memberService.gethouseIdByClientId(clientId);
		List<Long> lid = new ArrayList<Long>();
		lid.add(participantId);
		List<MemberVO> memberList = memberService.findMemberbyListId(lid,
			houseList);
		List<MemberVO> memberInfoList = this.paginationList(pageNO, pageSize,
			memberList);
		setAttribute("memberInfoList", memberInfoList);
		setAttribute("totalRows", new Integer(memberList.size()));
		this.getRequest().setAttribute("ownerId", participantId);
		this.getRequest().setAttribute("clientType", clientType);
		this.getRequest().setAttribute("clientId", clientId);
		return "initMainMemberBaseList";
	}

	/**
	 * 方法描述: 分页显示家庭成员信息
	 * @author 胡煦
	 * Create on:2008-6-3
	 * @param
	 * @return
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	// @Out(key = "memberInfoList")
	public String showMemberSampleInfoList(@Read(key = "key")
	String key) {
		this.getRequest().setAttribute("keyId", key);
		return "renderSampleMemberList";
	}

	/**
	 * 方法描述:获得成员所有关系类型
	 * @author 胡煦
	 * Create on:2008-6-11
	 * @param
	 * @return
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public List getReletionType() {
		return memberService.getReletionType();
	}

	/**
	 * 方法描述:初始化主页面成员修改页面
	 * @author 胡煦
	 * Create on:2008-6-10
	 * @param
	 * @return
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public String initMemberMEdit(@Read(key = "ownerId")
	Long ownerId, @Read(key = "paticpantId")
	Long paticpantId, @Read(key = "clientType")
	String clientType, @Read(key = "houseId")
	Long houseId, @Read(key = "clientId")
	Long clientId) {
		//		初始化下拉列表
		List<Long> cid = new ArrayList<Long>();
		cid.add(ownerId);
		List<ParticipantDO> lid = new ArrayList<ParticipantDO>();
		lid = memberService.getCustomerNameby(cid);
		setAttribute("customerNameList", lid);

		setAttribute("houseNumList", customerService.getCustomerHouseListById(ownerId,
				clientType));
		List certificateTypeList = getCertificateType();

		setAttribute("certificateTypeList", certificateTypeList);

		//获得指定的成员信息

		List memberListVO = memberService.findMemberBy(ownerId, houseId);
		for (int i = 0; i < memberListVO.size(); i++) {
			MemberVO memberVO = (MemberVO) memberListVO.get(i);
			if (memberVO.getParticipantId().equals(paticpantId)) {
				memberVO.setOldOwnerId(ownerId);
				memberVO.setOldOwnerName(memberVO.getOldOwnerName());
				memberVO.setOldHouseNumber(memberVO.getHouseNumber());
				getRequest().setAttribute("memberVO", memberVO);
				String interest = memberVO.getInterest();
				List<InterestVO> notSelectList = getAllInterestList();
				List<InterestVO> selectedInterestList = new ArrayList<InterestVO>();
				if(!BlankUtil.isBlank(interest))
				{
					String interestString = interest.substring(0, interest.length()-1);
					filtrateExistentInterest(interestString, notSelectList, selectedInterestList);
				}
				setAttribute("IndividualInterestes",selectedInterestList);
				setAttribute("interestes", notSelectList);
				break;
			}
		}

		//初始化下拉列表
		getRequest().setAttribute("certificateTypes", getCertificateType());
		setAttribute("reletionTypeList", getReletionType());
		return "initMemberMEdit";

	}

	/**
	 * Function: 初始化主页面新增成员信息
	 * @return
	 * Create author:胡煦
	 * Create on:2008-6-6
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public String initMemberMAdd(@Read(key = "ownerId")
	Long ownerId, @Read(key = "paticpantId")
	Long paticpantId, @Read(key = "clientType")
	String clientType, @Read(key = "clientId")
	Long clientId) {
		//		初始化下拉列表
		List<Long> cid = new ArrayList<Long>();
		cid.add(ownerId);
		List<ParticipantDO> lid = new ArrayList<ParticipantDO>();
		lid = memberService.getCustomerNameby(cid);
		setAttribute("customerNameList", lid);

		setAttribute("houseNumList", customerService.getCustomerHouseListById(ownerId,
				clientType));
		getRequest().setAttribute("certificateTypes", getCertificateType());
		setAttribute("reletionTypeList", getReletionType());
		List<InterestVO> listAll = getAllInterestList();

		getRequest().setAttribute("interestes", listAll);

		return "initMemberMAdd";
	}

	/**
	 * 方法描述:初始化成员修改页面
	 * @author 胡煦
	 * Create on:2008-6-10
	 * @param
	 * @return
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public String initMemberEdit(@Read(key = "keyId")
	String keyId, @Read(key = "ownerId")
	Long ownerId, @Read(key = "paticpantId")
	Long paticpantId, @Read(key = "houseId")
	Long houseId) {
		//		初始化下拉列表
		List<CustomerVO> cutomerList = this.getCustomerListForMemberCache();
		setAttribute("customerNameList", cutomerList);

		setAttribute("houseNumList", super.getCustomerHouseListCacheHerebyFlag(
				keyId, true));
		List certificateTypeList = getCertificateType();

		setAttribute("certificateTypeList", certificateTypeList);

		//		获得指定的成员信息

		List memberListVO = memberService.findMemberBy(ownerId, houseId);
		for (int i = 0; i < memberListVO.size(); i++) {
			MemberVO memberVO = (MemberVO) memberListVO.get(i);
			if (memberVO.getParticipantId().equals(paticpantId)) {
				memberVO.setOldOwnerId(ownerId);
				memberVO.setOldOwnerName(memberVO.getOwnerName());
				memberVO.setOldHouseNumber(memberVO.getHouseNumber());
				getRequest().setAttribute("memberVO", memberVO);
				String interest = memberVO.getInterest();
				List<InterestVO> notSelectList = getAllInterestList();
				List<InterestVO> selectedInterestList = new ArrayList<InterestVO>();
				if(!BlankUtil.isBlank(interest))
				{
					String interestString = interest.substring(0, interest.length()-1);
					filtrateExistentInterest(interestString, notSelectList, selectedInterestList);
				}
				setAttribute("IndividualInterestes",selectedInterestList);
				setAttribute("interestes", notSelectList);
				break;
			}
		}

		//初始化下拉列表
		getRequest().setAttribute("certificateTypes", getCertificateType());
		setAttribute("reletionTypeList", getReletionType());
		return "initMemberEdit";

	}

	/**
	 * Function:输入证件类型和证件号码后，需要确认该证件类型和号码是否能输入
	 * (如果对应的参与人已经是业主、租户、家庭成员则不能登记)。 <br>
	 * 如果可以输入,则提示用户是否导入。
	 * 如果不可以输入，则提示用户，说明该参与人已经是业主、
	 * 租户、或者是家庭成员，要求重新填写。 <br>
	 * @param certificateNo 证件号码
	 * @param certificateType 证件类型
	 * @param oldPid旧的参与人ID
	 * @param newPid新的参与人ID
	 * @param optionType 操作类型  isAddADD是新增中的新增 isAddEdit是新增中的修改 isEdit是修改
	 * @return
	 * Create author:yixianjian
	 * Create on:2008-7-4
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public String validateMemberAllowLoad(@Read IndividualVO individualVO,
			@Read(key="houseId")Long houseId){
		return this.validateIndividualLoadForRenderText(individualVO,houseId);
	}
	/**
	* Function:输入证件类型和证件号码后，
	* 需要确认该证件类型和号码标识的参与人信息是否能导入。
	* (如果对应的参与人已经是业主、租户、家庭成员则不能登记)。 <br>
	* 如果可以导入,则提示用户是否导入。
	* 如果不可以导入，则提示用户，进行相关说明，要求重新填写。<br>
	* 规则：对于局部变量flag的取值意义如下：<br>
	* <ul>
	* <li>true ： 验证通过</li>
	* <li>false ： 正常情况，说明是新的证件类型和号码，不导入也无任何提示</li>
	* <li>existCache ： 已经增加了的</li>
	* <li>001 : 已经是被选房的业主</li>
	* <li>002 : 已经是被选房的租户</li>
	* <li>003 或 004 : 已经是被选房的家庭成员</li>
	* </ul>
	* @param certificateNo 证件号码
	* @param certificateType 证件类型
	* @param oldIndividualId 原有的id
	* @param newIndividualId 新id
	* @param operateType 判断是修改还是新增
	* @param
	* Create author:yixianjian
	* Create on:2008-7-4
	* Edit author:付湘辉
	* Edit on:2008-10-24
	* Why:更改注释
	*/
	protected String validateIndividualLoadForRenderText(IndividualVO individualVO,Long houseId){
		String flag = CustomerAction.NORMAL_STATE;
		CustomerVO customerVo = this.customerService.queryParticipant(individualVO.getCertificateType().toString(), individualVO.getCertificateNo());
			if(!BlankUtil.isBlank(customerVo)){
				flag = this.queryHouseAndCustomerRelation(customerVo,houseId);
				if (!CustomerAction.NORMAL_STATE.equalsIgnoreCase(flag)) {
					this.renderText(flag);
					return flag;
				}
			}

		this.renderText(flag);
		return flag;
	}

	/** 功能描述:  <br>
	 * @param customerVo
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-7-30  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	*/
	protected String queryHouseAndCustomerRelation(CustomerVO customerVo,Long houseId) {
			Long participantId= customerVo.getParticipantId();
			String houseNumber = houseService.getFlatHouseById(houseId).getHouseNumber();
			CustomerHouseVO customerHouseVO = new CustomerHouseVO();
			customerHouseVO.setHouseNumber(houseNumber);
			String res = this.customerService.queryClientTypeByParticipantIdAhouse(participantId, customerHouseVO);//     -----到后台验证
			if (!BlankUtil.isBlank(res)) {
				return res;
			}
		return CustomerAction.NORMAL_STATE;
	}

	/**
	 * 修改家庭成员
	 * @param memberVO
	 * @return Long
	 * @roseuid 4821627101F2
	 */
	public Long editMember(@Read
	MemberVO memberVO, @Read(key = "ownerId")
	Long ownerId, @Read(key = "houseId")
	Long houseId) {
		memberVO.setCreaterId(getUserId());
		memberService.editMember(memberVO);
		this.renderText("true");
		return null;
	}

	/**
	 * 方法描述: 成员删除方法，改删除为逻辑删除，只解除关系表中的关系记录
	 * @author 胡煦
	 * Create on:2008-6-10
	 * @param
	 * @return
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */

	public Long deleteMember(@Read(key = "ownerId")
	Long ownerId, @Read(key = "paticpantId")
	Long paticpantId, @Read(key = "houseId")
	Long houseId) {
		return memberService
				.deleteMemberRel(ownerId, paticpantId, houseId);
	}

	/**
	 * @return the memberService
	 */
	public IMemberService getMemberService() {
		return memberService;
	}

	/**
	 * @param memberService the memberService to set
	 */
	public void setMemberService(IMemberService memberService) {
		this.memberService = memberService;
	}

}
